package com.ldoublem.loadingviewlib;


import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;


public class LVCircular extends Component implements Component.EstimateSizeListener, Component.DrawTask {


    private Paint mPaintCenter;
    private Paint mPaintRound;


    private float mWidth = 0f;
    private float mAnimatedValue = 0f;
    private int mStartAngle = 0;
    private float mMaxRadius = 4;
    AnimatorProperty mProgerssRotateAnim;

    public LVCircular(Context context) {
        this(context, null);
    }

    public LVCircular(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVCircular(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {

        setEstimateSizeListener(this);
        addDrawTask(this);

        mPaintCenter = new Paint();
        mPaintCenter.setAntiAlias(true);
        mPaintCenter.setStyle(Paint.Style.FILL_STYLE);
        mPaintCenter.setColor(Color.WHITE);
        mPaintRound= new Paint();
        mPaintRound.setAntiAlias(true);
        mPaintRound.setStyle(Paint.Style.FILL_STYLE);
        mPaintRound.setColor(Color.WHITE);


        mProgerssRotateAnim = this.createAnimatorProperty();
        mProgerssRotateAnim.rotate(360);
        mProgerssRotateAnim.setLoopedCount(-1);



    }

    public void setViewColor(int color) {
        mPaintCenter.setColor(new Color(color));
        invalidate();
    }
    public void setRoundColor(int color) {
        mPaintRound.setColor(new Color(color));
        invalidate();
    }




    public void startAnim() {
        stopAnim();
        mProgerssRotateAnim.setDuration(3500);
        mProgerssRotateAnim.start();
    }
    public void startAnim(int time) {
        stopAnim();
        mProgerssRotateAnim.setDuration(time);
        mProgerssRotateAnim.start();
    }
    public void stopAnim() {
        mProgerssRotateAnim.cancel();
    }


    @Override
    public boolean onEstimateSize(int i, int i1) {


        if (EstimateSpec.getSize(i) > getHeight())
            mWidth = EstimateSpec.getSize(i1);
        else
            mWidth = EstimateSpec.getSize(i);

        mMaxRadius = mWidth / 30f;
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        for (int i = 0; i < 9; i++) {
            float x2 = (float) ((mWidth / 2.f - mMaxRadius) * Math.cos(mStartAngle + 45f * i * Math.PI / 180f));
            float y2 = (float) ((mWidth / 2.f - mMaxRadius) * Math.sin(mStartAngle + 45f * i * Math.PI / 180f));
            canvas.drawCircle(mWidth / 2.f - x2, mWidth / 2.f - y2, mMaxRadius, mPaintRound);
        }

        canvas.drawCircle(mWidth / 2.f, mWidth / 2.f, mWidth / 2.f - mMaxRadius * 6, mPaintCenter);
    }
}
